<template>
  <div class="recommend">
    <comp-title class="titile">
      <template v-slot:ctitle> 推荐音乐 </template>
    </comp-title>

    <ul class="recommendMusic-list">
      <li v-for="item in recommendMusic" :key="item.id">
        <a href="javascript:;">
          <comp-image
            :name="item.name"
            :play-count="item.playCount"
            :pic-url="item.picUrl"
          ></comp-image>
        </a>
      </li>
    </ul>

    <comp-title class="titile">
      <template v-slot:ctitle> 最新音乐 </template>
    </comp-title>
    <ul class="newMusic-list">
      <li class="music-box" v-for="(item, i) in newMusic" :key="item.id">
        <div class="left">
          <h5 class="title">{{ item.name }}</h5>
          <p>
            <span class="icon"></span>
            <span class="text">{{ item.author }}</span>
          </p>
        </div>
        <div class="right">
          <!-- fa-pause-circle-o -->
          <span
            :class="[
              'icon',
              'fa',
              item.isPlay == false ? 'fa-pause-circle-o' : 'fa-play-circle-o',
            ]"
            @click="addClick(i, item.id)"
          ></span>
        </div>
      </li>
    </ul>

    <transition
      :duration="{ enter: 800, leave: 150 }"
      enter-class="animate__animated animate__lightSpeedInLeft"
      enter-to-class="animate__animated animate__lightSpeedInLeft"
      leave-class="animate__animated animate__lightSpeedOutRight"
      leave-to-class="animate__animated animate__lightSpeedOutRight"
    >
      <footer v-if="id">
        <comp-player :cid="id" :cimg="picUrl" :cname="name"></comp-player>
      </footer>
    </transition>
  </div>
</template>

<script>
import compTitle from "@/components/comp-title.vue";
import compImage from "@/components/comp-image.vue";
import CompPlayer from "@/components/comp-player.vue";
export default {
  components: {
    compTitle,
    compImage,
    CompPlayer,
  },
  name: "Recommend",
  data() {
    return {
      index: -1,
      recommendMusic: [],
      // isPlay: true,
      newMusic: [],
      id: 0,
      name: "",
      picUrl: "",
    };
  },

  methods: {
    _initRecommend() {
      let _this = this;
      let pro = axios.get("/personalized", {
        params: {
          limit: 6,
        },
      });
      pro.then((response) => {
        let res = response.data;
        if (res.code == 200) {
          let arr = [];
          res.result.forEach((item) => {
            let obj = {
              copywriter: item.copywriter,
              id: item.id,
              name: item.name,
              picUrl: item.picUrl,
              playCount: item.playCount,
            };

            arr.push(obj);
          });
          _this.recommendMusic = arr;
        }
      });
    },
    _initNewMusic() {
      let _this = this;
      let pro = axios.get("/personalized/newsong");
      pro.then((response) => {
        let res = response.data;
        if (res.code == 200) {
          let arr = [];
          res.result.forEach((item) => {
            let obj = {
              id: item.id,
              name: item.name,
              author: item.song.artists[0].name,
              picUrl: item.picUrl,
              isPlay: true,
            };
            arr.push(obj);
          });
          _this.newMusic = arr;
        }
      });
    },
    addClick(i, tid) {
      this.newMusic.forEach((item, index) => {
        if (i != index) {
          item.isPlay = true;
        }
      });

      if (this.newMusic[i].isPlay) {
        this.index = i;
      } else {
        this.index = -1;
      }
      this.newMusic[i].isPlay = !this.newMusic[i].isPlay;

      //设置id
      this.id = this.newMusic[i].id;
      this.name = this.newMusic[i].name;
      this.picUrl = this.newMusic[i].picUrl;
    },
  },
  mounted() {
    this._initRecommend();
    this._initNewMusic();
  },
};
</script>

<style lang="less" scoped>
@bgColor: #5ab8ee;
@bdColor: #2eb4ff;
.recommend {
  position: relative;
  overflow: hidden;
  padding-bottom: 70px;
  .recommendMusic-list {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    align-content: flex-start;
    li {
      flex-grow: 1;
      flex-basis: 33.333%;
      flex-shrink: 0;
      a {
        display: block;
        width: 97%;
      }
    }
  }

  .newMusic-list {
    .music-box {
      width: 100%;
      display: flex;
      padding: 8px 15px;
      border-bottom: 1px dashed #ccc;
      .left {
        flex-grow: 1;
        h5.title {
          font-size: 18px;
          font-weight: 500;
          color: #000;
          padding-bottom: 5px;
        }
        p {
          color: #666;
          font-size: 0;
          .icon {
            display: inline-block;
            width: 24px;
            height: 16px;
            background-image: url(../assets/sprites.png);
            background-position: 0 0;
            vertical-align: middle;
            margin-right: 5px;
          }
          .text {
            font-size: 14px;
            vertical-align: middle;
          }
        }
      }
      .right {
        flex-basis: 50px;
        height: 44px;
        line-height: 44px;
        font-size: 0;
        padding-right: 10px;
        .icon {
          font-size: 30px;
          vertical-align: middle;
        }
        .icon.fa {
          color: #5ab8ee;
        }
      }
    }
  }

  footer {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 70px;
    background-color: rgba(0, 0, 0, 0.2);
    border-top: 2px solid @bdColor;
  }
}
</style>